using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Main.controls;
namespace Main.users
{
    public partial class profile : System.Web.UI.Page
    {
        protected ShowFriend myShowFriend;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //First, we'll see if the user is trying to view a profile page
            if (Request.QueryString["user"] != null)
            {
                try
                {
                    MembershipUser user;
                    user = Membership.GetUser(Request.QueryString["user"]);
                    ShowProfile myProfile = (ShowProfile)LoadControl("/controls/ShowProfile.ascx");
                    myProfile.UserID = (Guid)user.ProviderUserKey;
                    this.mainPanel.Controls.Add(myProfile);

                    //finally, if the user is logged in, we want to 
                    //allow them to create a friendship. We also want to test to 
                    //see if the friendship already exists.

                    if (this.User.Identity.IsAuthenticated)
                    {
                        AddFriend addThisUser = (AddFriend)LoadControl("/controls/AddFriend.ascx");
                        addThisUser.PostBackURL = "/users/profile.aspx?user=" + Request.QueryString["user"];
                        addThisUser.UserID = (Guid)Membership.GetUser().ProviderUserKey;
                        addThisUser.FriendID = (Guid)user.ProviderUserKey;
                        this.mainPanel.Controls.Add(addThisUser);
                    }
                }
                catch (Exception userE)
                {
                    this.ContentHeader.Text = "User not found";
                }
            }
            else
            {
                //Well, then lets see if they are logged in. Otherwise, they
                //have no business being here.
                if (this.User.Identity.IsAuthenticated)
                {
                    this.ContentHeader.Text = "Welcome, user!";
                    FriendCollection myFriends = new FriendCollection();
                    myFriends.FillByUserID((Guid)Membership.GetUser().ProviderUserKey);
                    
                    foreach (Friend friend in myFriends)
                    {
                        myShowFriend = (ShowFriend)LoadControl("/controls/ShowFriend.ascx");
                        myShowFriend.FriendID = friend.FriendID;
                        this.mainPanel.Controls.Add(myShowFriend);
                    }
                }
                else
                {
                    this.ContentHeader.Text = "You need to be logged in.";
                }
            }
        }
    }
}
